using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XFGameFramework;
using XFGameFramework.ArchiveSystem;
using XFGameFramework.BuffSystem;

public class GameSaveController : Controller
{
    GameController gameController => Module.LoadController<GameController>();
    TimeController timeController => Module.LoadController<TimeController>();
    PlayerController playerController => Module.LoadController<PlayerController>();
    /// <summary>
    /// 创建游戏存档
    /// </summary>
    /// <param name="index"></param>
    public void CreateGameSave(int index)
    {
    
        TimeModel timeModel = ArchiveManager.Create<TimeModel>(index+5);
        PlayerModel playerModel = ArchiveManager.Create<PlayerModel>(index);
        if(index == 0)//每次创建0号存档必然意味着0号存档被清除，需要重新引用
        {
            timeController.Reload();
            playerController.Reload();
        }
        
        
        // GameSaveModel gameSaveModel = ArchiveManager.Create<GameSaveModel>(index);

    }
    

    public TimeModel GetTimeModel(int index)
    {
        if (ArchiveManager.Get<TimeModel>(index+5) != null)
        {
            return ArchiveManager.Get<TimeModel>(index+5);
        }
        else //(ArchiveManager.Get<GameSaveModel>(index) == null)
        { return null; }
    }
    public PlayerModel GetPlayerModel(int index)
    {
        if (ArchiveManager.Get<PlayerModel>(index) != null)
        {
            return ArchiveManager.Get<PlayerModel>(index);

        }
        else //(ArchiveManager.Get<PlayerModel>(index) == null)
        { return null; }
    }
    public void CreateEnemyModel(int index)
    {   ArchiveManager.Delete(index);
        ArchiveManager.Create<EnemyModel>(index);
    }
    public EnemyModel GetEnemyModel(int index)
    {
            return ArchiveManager.Get<EnemyModel>(index);
    }
    
        


    // /// <summary>
    /// 保存游戏存档
    /// </summary>
    /// <param name="index"></param>
    public void SaveGameSave(int index)
    {
        ArchiveManager.Save(index+5);
        ArchiveManager.Save(index);
        Debug.Log($"保存存档{index}");
    }
    /// <summary>
    /// 加载游戏存档
    /// </summary>
    /// <param name="index"></param>
    // public void LoadGameSave(int index)
    // {
    //     GameSaveModel gameSaveModel = ArchiveManager.Get<GameSaveModel>(index);
    //     if (gameSaveModel != null)
    //     {
    //         PlayerModel playerModel = gameSaveModel.playerModel;
    //         Module.AddModel(playerModel);
    //     }
    // }
    /// <summary>
    /// 删除游戏存档
    /// </summary>
    /// <param name="index"></param>
    public void DeleteGameSave(int index)
    {
        ArchiveManager.Delete(index);
        ArchiveManager.Delete(index+5);
        
    }
    public bool GetExists()
    {
        for (int i = 1; i < 5; i++)
        {
            if (GetTimeModel(i) != null) return true;
        }
        return false;
    }
    public int GetArchiveCount()
    {
        return ArchiveManager.AllIds.Count/2;
    }

    /// <summary>
/// 覆盖游戏存档
/// </summary>
/// <param name="newindex"></param>x新存档的索引
/// <param name="oldindex"></param>当前存档号
    public void CoverGameSave(int newindex,int oldindex= 0)
    {
       
        TimeModel oldTimeModel = GetTimeModel(oldindex);
        PlayerModel oldPlayerModel = GetPlayerModel(oldindex);

        TimeModel TempTimeModel = new TimeModel();
        PlayerModel TempPlayerModel = new PlayerModel();
           
        //只获取数值而不获取引用
        //用temp变量保存旧存档值
        Period currentPeriod = oldTimeModel.currentPeriod;
        int days = oldTimeModel.days;
        int hours = oldTimeModel.hours;
        int minutes = oldTimeModel.minutes;
        int remainingTime = oldTimeModel.remainingTime;

        string name = oldPlayerModel.name;
        int Agility = oldPlayerModel.Agility;
        int currentHp = oldPlayerModel.currentHp;
        int currentNeatness = oldPlayerModel.currentNeatness;
        int currentStrength = oldPlayerModel.currentStrength;
        List<BuffBase> Buffs = oldPlayerModel.Buffs;
        int level = oldPlayerModel.level;
        int maxHp = oldPlayerModel.maxHp;
        int neatness = oldPlayerModel.neatness;
        int physique = oldPlayerModel.physique;
        int strength = oldPlayerModel.strength;  
        List<int> skillsID = oldPlayerModel.skillsID;
        List<BuffBase> buffsID = oldPlayerModel.Buffs;
        List<int>skillsCD = oldPlayerModel.skillsCD;

        //保存旧存档使得旧存档获得新数据，创建新存档
        
        DeleteGameSave(newindex);
        CreateGameSave(newindex);
        oldTimeModel = GetTimeModel(oldindex);
        oldPlayerModel = GetPlayerModel(oldindex);

        TimeModel newTimeModel = GetTimeModel(newindex);
        PlayerModel newPlayerModel = GetPlayerModel(newindex);
        //新存档获取已保存存档数据
        newTimeModel.currentPeriod = oldTimeModel.currentPeriod;
        newTimeModel.days = oldTimeModel.days;
        newTimeModel.hours = oldTimeModel.hours;
        newTimeModel.minutes = oldTimeModel.minutes;
        newTimeModel.remainingTime = oldTimeModel.remainingTime;

        newPlayerModel.name = oldPlayerModel.name;
        newPlayerModel.Agility = oldPlayerModel.Agility;
        newPlayerModel.currentHp = oldPlayerModel.currentHp;
        newPlayerModel.currentNeatness = oldPlayerModel.currentNeatness;
        newPlayerModel.currentStrength = oldPlayerModel.currentStrength;
        newPlayerModel.Buffs = oldPlayerModel.Buffs;
        newPlayerModel.level = oldPlayerModel.level;
        newPlayerModel.maxHp = oldPlayerModel.maxHp;
        newPlayerModel.neatness = oldPlayerModel.neatness;
        newPlayerModel.physique = oldPlayerModel.physique;
        newPlayerModel.strength = oldPlayerModel.strength; 
        newPlayerModel.skillsID = oldPlayerModel.skillsID;
        newPlayerModel.Buffs = oldPlayerModel.Buffs;
        newPlayerModel.skillsCD = oldPlayerModel.skillsCD;
        SaveGameSave(newindex);

        GameController gameController = Module.LoadController<GameController>();
        gameController.SetArchiveIndex(newindex);
        Debug.Log($"当前存档已被设置为{newindex}");
        
        //旧存档获取到最初值
        oldTimeModel.currentPeriod = currentPeriod;
        oldTimeModel.days = days;
        oldTimeModel.hours = hours;
        oldTimeModel.minutes = minutes;
        oldTimeModel.remainingTime = remainingTime;

        oldPlayerModel.name = name;
        oldPlayerModel.Agility = Agility;
        oldPlayerModel.currentHp = currentHp;
        oldPlayerModel.currentNeatness = currentNeatness;
        oldPlayerModel.currentStrength = currentStrength;
        oldPlayerModel.Buffs = Buffs;
        oldPlayerModel.level = level;
        oldPlayerModel.maxHp = maxHp;
        oldPlayerModel.neatness = neatness;
        oldPlayerModel.physique = physique;
        oldPlayerModel.strength = strength; 
        oldPlayerModel.skillsID = skillsID;
        oldPlayerModel.Buffs = buffsID;
        oldPlayerModel.skillsCD = skillsCD;

    }
}